﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Autofac;
using Autofac.Integration.Mvc;
using Rezene.Web.Business;
using Rezene.Web.Business.Amazon;

namespace Rezene.Web
{
    public class AutfacConfiguration
    {
        public static void Initialize()
        {
            var builder = new ContainerBuilder();
            var currentAssembly = Assembly.GetExecutingAssembly();
            /*            
            builder.RegisterAssemblyTypes(currentAssembly)
                   .Where(x => x.Name.EndsWith("Repository", StringComparison.OrdinalIgnoreCase) || x.Name.EndsWith("Factory", StringComparison.OrdinalIgnoreCase) || x.Name.EndsWith("Service", StringComparison.OrdinalIgnoreCase))
                   .AsImplementedInterfaces()
                   .SingleInstance()
                   .PropertiesAutowired();
            */
            builder.RegisterType<ConfigurationRepository>().As<IConfigurationRepository>().AsImplementedInterfaces().SingleInstance().PropertiesAutowired();
            builder.RegisterType<AmazonFactory>().As<IAmazonFactory>().AsImplementedInterfaces().SingleInstance().PropertiesAutowired();
            builder.RegisterType<AmazonRepository>().As<IAmazonRepository>().AsImplementedInterfaces().SingleInstance().PropertiesAutowired();                        
            builder.RegisterControllers(currentAssembly).PropertiesAutowired();

            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            //GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
            //ContainerFactory.Container = container;

        }
    }
}